package org.example.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.example.constant.Constants;
import org.example.model.UserInfo;
import org.example.utils.JsonUtils;
import org.example.utils.Mail;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class UserQueueListener {
    @Autowired
    private Mail mail;
    //这里的新写法
    @RabbitListener(queues = Constants.USER_QUEUE_NAME)
    public void halderMessage(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try{
            String body=new String(message.getBody());
            UserInfo userInfo = JsonUtils.parseJson(body, UserInfo.class);
            //这里就开始发送邮件了
            mail.send(userInfo.getEmail(),"恭喜加入博客社区",buildContent(userInfo.getUserName()));
            log.info("发送邮件完毕111111111111111111111111111111111111111111111");
            channel.basicAck(deliveryTag,true);
        }catch (Exception e){
            channel.basicNack(deliveryTag,true,true);
            log.info("邮件发送失败mail send error "+e);
        }
    }
    private String buildContent(String userName){
        StringBuilder builder = new StringBuilder();
        builder.append("尊敬的").append(userName).append(", 您好!").append("<br/>");
        builder.append("感谢您注册成为我们博客社区的一员! 我们很高新您加入我们的大家庭!<br/>");
        builder.append("您的注册信息如下: 用户名: ").append(userName).append("<br/>");
        builder.append("为了确保您的账⼾安全，请妥善保管您的登录信息. 如果使⽤过程中, 遇到任何问题, 欢迎联系我们的⽀持团队. XXXX@bite.com <br/>");
        builder.append("再次感谢您的加⼊，我们期待看到您的精彩内容！<br/>")
                .append("最好的祝愿<br/>")
                .append("⽐特博客团队").toString();
        return builder.toString();
    }
}
